package org.fioma.search;

import java.util.ArrayList;
import java.util.List;

public class MatchReadCollectionImpl implements MatchReadCollection {

//	Read read;
	Match[] matches;
	int size = 0;
	int c, j, k;

	ArrayList<Match> matchesK = new ArrayList<Match>();
	ArrayList<Integer> junctionsIndex = new ArrayList<Integer>();
	ArrayList<Integer> gapsIndex = new ArrayList<Integer>();
	int lastMismatch = -1;

	public MatchReadCollectionImpl(int maxSize, int k, int c, int j) {
		matches = new Match[maxSize];
		this.k = k;
		this.c = c;
		this.j = j;
	}

	@Override
	public Match getMatch(int i) {
		return matches[i];
	}

	@Override
	public Match[] getMatches() {
		return matches;
	}

	@Override
	public void addMatchSorted(Match match) {
		matches[size] = match;

		if (match.getLength() >= k) {
			if (lastMismatch >= 0 && match.getStartPositionWord() >= (lastMismatch - c)) {
				if (match.getStartPositionWord() >= (lastMismatch + j)) {
					gapsIndex.add(size);
				} else {
					junctionsIndex.add(size);
				}
			}
			matchesK.add(match);
			lastMismatch = match.getMismatchPositionWord();
		}

		size++;
	}

	@Override
	public int size() {
		return size;
	}

	@Override
	public void clear() {
		size = 0;
		junctionsIndex.clear();
		gapsIndex.clear();
		matchesK.clear();
		lastMismatch = -1;
	}

//	@Override
//	public Read getRead() {
//		return read;
//	}
//
//	@Override
//	public void setRead(Read read) {
//		this.read = read;
//	}
//
	public int getInitialPositionFirstMatch() {
		if (matchesK.isEmpty()) {
			return -1;
		} else {
			return matchesK.get(0).getStartPositionWord();
		}
	}

	public int getMismatchPositionLastMatch() {
		return lastMismatch;
	}

	public List<Integer> getJunctionIndexes() {
		return junctionsIndex;
	}

	public List<Integer> getGapIndexes() {
		return gapsIndex;
	}

}
